home *** CD-ROM | disk | FTP | other *** search
- /*================================================================================
- StringUtilities.c
-
- ©1991 Greg Anderson
- greggor@apple.com
-
- FREE DISTRIBUTION
-
- The possessor of this source code may use any portion of
- it for any purpose, and I place no restriction on its
- redistribution.
-
- Convienient string utility routines. See the macros defined
- in stringUtilities.h, too.
- ================================================================================*/
- #include "StringUtilities.h"
- #include <string.h>
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- /*----------------------------------------------------------------------
- Copy a Pascal string into a C string
- ----------------------------------------------------------------------*/
- void PtoCcpy( char* cstr, Str255 pstr )
- {
- unsigned short i = (unsigned char)pstr[0];
-
- cstr[i] = 0;
- while( i > 0 )
- {
- cstr[i - 1] = pstr[i];
- --i;
- }
- }
-
- /*----------------------------------------------------------------------
- Copy a C string into a Pascal string
- ----------------------------------------------------------------------*/
- void CtoPcpy( Str255 pstr, char* cstr )
- {
- int i = 1;
-
- while( (*cstr) && (i < 255) )
- pstr[i++] = *cstr++;
- pstr[0] = i - 1;
- }
-
- /*----------------------------------------------------------------------
- Compare a Pascal string with a C string
- ----------------------------------------------------------------------*/
- short PtoCcmp( Str255 pstr, char* cstr )
- {
- char tmp[256];
-
- PtoCcpy( tmp, pstr );
- return( strcmp( tmp, cstr ) );
- }
-
- /*----------------------------------------------------------------------
- Copy one pascal string into another
- ----------------------------------------------------------------------*/
- pascal void pstrcpy( Str255 dest, Str255 src )
- {
- unsigned char* dp;
- unsigned char* sp;
- short i;
- short max;
-
- sp = (unsigned char*)src;
- dp = (unsigned char*)dest;
- max = *sp;
-
- for( i = 0; i <= max ; ++i )
- *dp++ = *sp++;
- }
-
- /*----------------------------------------------------------------------
- Copy one pascal string onto the end of another
- ----------------------------------------------------------------------*/
- pascal void pstrcat( Str255 dest, Str255 src )
- {
- unsigned char* dp;
- unsigned char* sp;
- unsigned char* cp;
- short i;
- short max;
-
- sp = (unsigned char*)src;
- dp = (unsigned char*)dest;
- max = *sp;
- /*
- // Adjust past length & chars already in dest
- */
- cp = dp + *dp + 1;
- ++sp;
- /*
- // Determine how many characters we can really copy
- */
- if( max + *dp > 255 )
- max = 255 - *dp;
- *dp += max;
-
- for( i = 0; i < max ; ++i )
- *cp++ = *sp++;
- }
-
- /*----------------------------------------------------------------------
- Compare two Pascal strings
- ----------------------------------------------------------------------*/
- short pstrcmp( unsigned char* a, unsigned char* b )
- {
- short len = ( *a < *b ? *a : *b );
- short lenEqual = ( *a > *b ) - ( *a < *b );
- short equalityTest;
-
- while( len )
- {
- ++a;
- ++b;
- equalityTest = ( *a > *b ) - ( *a < *b );
-
- if( equalityTest )
- return equalityTest;
-
- --len;
- }
-
- return lenEqual;
- }
-
- /*-------------------------------------------------------------------
- 'Partial' string compare - check if s2 appears at the beginning
- of s1.
-
- Identical to 'strncmp(s1,s2,strlen(s2));', with the bonus feature
- of being case-insensitive.
- -------------------------------------------------------------------*/
- short partialstrcmp( register char* s1, register char* s2)
- {
- register char c1,
- c2;
-
- while( c2 = *s2++ )
- {
- c1 = *s1++;
- c1 = ToUpper(c1);
- c2 = ToUpper(c2);
- if( c1 < c2 ) return(-1);
- if( c1 > c2 ) return( 1);
- }
- return(0);
- }
-
- /*-------------------------------------------------------------------
- Convert an ascii number into a string, advance the string ptr
- past the number
- -------------------------------------------------------------------*/
- short ScanNumberInString( char** line )
- {
- short n = 0;
- while( (**line >= '0') && (**line <= '9') )
- {
- n = n * 10 + (**line - '0');
- ++(*line);
- }
- return n;
- }
-
- /*-------------------------------------------------------------------
- Convert an ascii number into a string
- -------------------------------------------------------------------*/
- short NumberInString( char* line )
- {
- return( ScanNumberInString( &line ) );
- }
-
- /*-------------------------------------------------------------------
- Copy characters from **line into the specified pstr
- -------------------------------------------------------------------*/
- void ScanTextInString( char** line, Str255 pstr, Boolean terminateAtSpace )
- {
- short len = 0;
-
- while( (len < 256) && ( (**line > ' ') || ( (**line == ' ') && (terminateAtSpace == false)) ) )
- {
- ++len;
- pstr[len] = **line;
- (*line)++;
- }
- pstr[0] = len;
- }
-
- /*-------------------------------------------------------------------
- Copy the next word from **line into the specified pstr
- -------------------------------------------------------------------*/
- void ScanWordInString( char** line, Str255 pstr )
- {
- ScanTextInString( line, pstr, true );
- }
-
- /*-------------------------------------------------------------------
- Copy the next line from **line into the specified pstr
- -------------------------------------------------------------------*/
- void ScanLineInString( char** line, Str255 pstr )
- {
- ScanTextInString( line, pstr, false );
- }
-
- /*-------------------------------------------------------------------
- Add a hex character to a string. Always adds 1 character
- -------------------------------------------------------------------*/
- void AddHexCharToString( char* where, unsigned short hexNumber )
- {
- hexNumber &= 0xF;
-
- if( hexNumber < 10 )
- *where = '0' + hexNumber;
- else
- *where = 'A' + (hexNumber - 10);
- }
-
- /*-------------------------------------------------------------------
- Add a hex byte to a string. Always adds 2 characters
- -------------------------------------------------------------------*/
- void AddHexByteToString( char* where, unsigned short hexNumber )
- {
- AddHexCharToString( where, hexNumber >> 4 );
- AddHexCharToString( where + 1, hexNumber & 0xF );
- }
-
- /*-------------------------------------------------------------------
- Add a hex shortword to a string. Always adds 4 characters
- -------------------------------------------------------------------*/
- void AddHexShortToString( char* where, unsigned short hexNumber )
- {
- AddHexByteToString( where, hexNumber >> 8 );
- AddHexByteToString( where + 2, hexNumber & 0xFF );
- }
-
- /*-------------------------------------------------------------------
- Add a hex longword to a string. Always adds 8 characters
- -------------------------------------------------------------------*/
- void AddHexLongToString( char* where, unsigned long hexNumber )
- {
- AddHexShortToString( where, hexNumber >> 16 );
- AddHexShortToString( where + 4, hexNumber & 0xFFFF );
- }
-